package com.example.mymusicdisplay.view.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mymusicdisplay.MusicManager;
import com.example.mymusicdisplay.R;
import com.example.mymusicdisplay.data.MusicData;
import com.example.mymusicdisplay.databinding.LocalMusicItemBinding;
import com.example.mymusicdisplay.utils.LiveDataBus;

import java.util.ArrayList;
import java.util.List;

/**
 * author: ts
 * created on: 2021/10/9 下午4:51
 * description:
 */
public class LocalMusicAdapter extends RecyclerView.Adapter<LocalMusicAdapter.MyViewHolder> {
    private static final String TAG = "LocalMusicAdapter";

    private List<MusicData> mMusicDataList = new ArrayList<>();

    private Context mContext;
    private LifecycleOwner mOwner;
    private int mCurrentPosition;

    private LocalMusicItemBinding mLocalMusicItemBinding;

    public LocalMusicAdapter(Context context, LifecycleOwner lifecycleOwner) {
        mContext = context;
        mOwner = lifecycleOwner;
    }

    public void setMusicDataList(List<MusicData> musicDataList) {
        Log.d(TAG, "setMusicDataList: ");
        if (musicDataList != null) {
            mMusicDataList.clear();
            mMusicDataList.addAll(musicDataList);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public LocalMusicAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mLocalMusicItemBinding = DataBindingUtil
            .inflate(LayoutInflater.from(mContext), R.layout.local_music_item, parent, false);
        return new MyViewHolder(mLocalMusicItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull LocalMusicAdapter.MyViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            Log.d(TAG, "onBindViewHolder: ");
            holder.binding(mMusicDataList.get(position), position);
        }
    }

    @Override
    public int getItemCount() {
        return mMusicDataList == null ? 0 : mMusicDataList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        LocalMusicItemBinding mBinding;

        public MyViewHolder(@NonNull LocalMusicItemBinding localMusicItemBinding) {
            super(localMusicItemBinding.getRoot());
            mBinding = localMusicItemBinding;
        }

        public void binding(MusicData musicData, int position) {
            Log.d(TAG, "binding: musicData = " + musicData);
            mBinding.localMusicName.setText(musicData.getSingName());
            mBinding.localMusicSinger.setText(musicData.getSinger());
            mBinding.localMusic.setOnClickListener(view -> {
                Log.d(TAG, "onBindViewHolder: musicData = " + musicData);
                LiveDataBus.get().with("position").postValue(position);
                MusicManager.getInstance().playPositionMusic(position);
            });
            LiveDataBus.get().with("position", Integer.class).observe(mOwner, new Observer<Integer>() {
                @Override
                public void onChanged(Integer currentPosition) {
                    if (currentPosition == position) {
                        Log.d(TAG, "onChanged: currentPosition == position");
                        mBinding.currentMusic.setVisibility(View.VISIBLE);
                    } else {
                        Log.d(TAG, "onChanged: currentPosition != position");
                        mBinding.currentMusic.setVisibility(View.GONE);
                    }
                }
            });
        }
    }
}
